package com.jackrain.nea.fc.dl.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Mapper
public interface ViewDLTranPlanToF4Mapper {

    @SelectProvider(type = ViewDLTranPlanSql.class, method = "findTranPlanData")
    List<HashMap> selTranPlanData(@Param("id") Object[] id,
                                  @Param("status") Integer status,
                                  @Param("isactive") String isactive,
                                  @Param("stopstatus") Integer stopstatus);

    @SelectProvider(type = ViewDLTranPlanSql.class, method = "findTranPlanItemData")
    List<HashMap> selTranPlanItemData(@Param("id") Long id);

    //获取未执行门店数量
    @Select("SELECT DISTINCT SUM_QTY_STORE_EXEC_PRE  FROM DL_B_TRAN_PLAN  WHERE BILL_NO=#{billno}")
    long  selTranPlanVoidStoreExecPre(@Param("billno") String billno);

    class ViewDLTranPlanSql {

        public String findTranPlanData(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            Integer status = (Integer) map.get("status");
            String isactive = (String) map.get("isactive");
            Integer stopstatus = (Integer) map.get("stopstatus");
            return new SQL() {
                {
                    SELECT("ID,BILL_NO,BILL_DATE,CP_C_ORIG_ECODE,CP_C_DEST_ECODE,FROM_BILL_NO,BILL_TYPE," +
                            "REMARK,SUM_QTY_STORE,MODIFIERNAME,MODIFIERENAME,MODIFIEDDATE,BILL_SOURCE");
                    FROM("DL_B_TRAN_PLAN");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                    if (null != status) {
                        WHERE("STATUS = " + status);
                    }
                    if (null != isactive) {
                        WHERE("ISACTIVE = '" + isactive + "'");
                    }
                    if (null != stopstatus) {
                        WHERE("STOPSTATUS = " + stopstatus);
                    }
                }
            }.toString();
        }

        public String findTranPlanItemData(Map<String, Object> map) {
            Long id = (Long) map.get("id");
            return new SQL() {
                {
                    SELECT("CP_C_DEST_ECODE,PS_C_SKU_ECODE,QTY_EXEC_PLAN,QTY_EXEC,QTY_STORE");
                    FROM("DL_B_TRAN_PLAN_ITEM");
                    WHERE("DL_B_TRAN_PLAN_ID =" + id);
                }
            }.toString();
        }


    }


}
